import axios from "axios";

// const Cookie = 'kdid=web_b10e108f31446f04cca9cb7183508a69; didv=1707213086614; ktrace-context=1|MS43NjQ1ODM2OTgyODY2OTgyLjE5MTgyNTk0LjE3MTA4MTg5MDk1MzcuNDU0OTU0Ng==|MS43NjQ1ODM2OTgyODY2OTgyLjk5MzM3OTEzLjE3MTA4MTg5MDk1MzcuNDU0OTU0Nw==|0|graphql-server|webservice|false|NA; userId=865484597; kuaishou.server.web_ph=87aae06bc7ec0282b72bb197c50d1a3a7831';
const Cookie = 'kpf=PC_WEB; clientid=3; did=web_b10e108f31446f04cca9cb7183508a69; didv=1707213086614; ktrace-context=1|MS43NjQ1ODM2OTgyODY2OTgyLjE5MTgyNTk0LjE3MTA4MTg5MDk1MzcuNDU0OTU0Ng==|MS43NjQ1ODM2OTgyODY2OTgyLjk5MzM3OTEzLjE3MTA4MTg5MDk1MzcuNDU0OTU0Nw==|0|graphql-server|webservice|false|NA; userId=865484597; kpn=KUAISHOU_VISION; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABu0ke4ykFlrUkvd1W24UkXw2IUv-hNQal8QlaNFs3Xh9BVNAGvB9JqLk2Ipn-TMzTCN8_7l8eIJmRXbygr8bVz6qxOBXGe_fzJaZnf0Z7dHkqLEoLXpAZ4n42TIhGaDmHCcSwWPkn_0i_yHWPvptQaK_-_rC_RICOU-fjYM3czefJUc-GHlCZAFrN4HuHN0cNpZ1dLztqpPAflYw_XXC43xoS5Uf6LooXNTrpm4StA9DDXaoVIiAP79P2g3jS5ShwStKlIh7h3kfQosXPj9XI9eLYSoUY3ygFMAE; kuaishou.server.web_ph=e5d8c8b8da3c88bd96ab0707387ba9989f7a';
const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46";

const addComment = (photoId: string, photoAuthorId: string, content: string,expTag: string) => {
    const url = 'https://www.kuaishou.com/graphql';

    const headers = {
        "User-Agent": userAgent,
        Cookie
    };

    const data = {
        operationName: "visionAddComment",
        variables: {
            photoId,
            photoAuthorId,
            content,
            expTag
        },
        "query": "mutation visionAddComment($photoId: String, $photoAuthorId: String, $content: String, $replyToCommentId: ID, $replyTo: ID, $expTag: String) {\n  visionAddComment(photoId: $photoId, photoAuthorId: $photoAuthorId, content: $content, replyToCommentId: $replyToCommentId, replyTo: $replyTo, expTag: $expTag) {\n    result\n    commentId\n    content\n    timestamp\n    status\n    __typename\n  }\n}\n"
    };

    return axios.post(url, data, { headers });
}

const getOwnerInfo = async (photoId: string) => {
    const url = `https://www.kuaishou.com/short-video/${photoId}`;

    const headers = {
        "User-Agent": userAgent,
        Cookie
    };

    const res = await axios.get(url, { headers });
    const str = res.data;
    let regex = /"expTag":"([^"]*)"/; // 正则表达式匹配 "expTag":"任意字符，直到遇到另一个双引号"
    let match = regex.exec(str); // 执行匹配操作
    let expTag ='';
    if (match?.length) {
        expTag = match[1];
    }
    let photoAuthorId ='';
    regex = /"VisionVideoDetailAuthor:([^"]*)"/; // 正则表达式匹配 "expTag":"任意字符，直到遇到另一个双引号"
    match = regex.exec(str); // 执行匹配操作
    if (match?.length) {
        photoAuthorId = match[1];
    }

    return {
        expTag: expTag.replace('\\u002F', '/'),
        photoAuthorId
    };
}

const main = async () => {
    const photoId = '3xcsvwg5xx4apwq';
    const ownerInfo = await getOwnerInfo(photoId);
    const res = await addComment(photoId, ownerInfo.photoAuthorId, "您好", ownerInfo.expTag);
    if (res.data.data && res.data.data.visionAddComment && res.data.data.visionAddComment.status === 'success') {
        console.log('评论成功');
    } else {
        console.log(res.data);
    }

};

main();























// 